<template>
    <van-nav-bar
        title="添加收货地址"
        left-text="返回"
        left-arrow
        @click-left="onBack"
    />

    <van-address-edit
        :area-list="areaList"
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
    />

</template>

<script setup>
import { ref, getCurrentInstance, reactive} from 'vue';
import { areaList } from '@vant/area-data';

const { proxy } = getCurrentInstance();
const business = reactive(proxy.$cookies.get('business') ?? {});

const onSave = async (values) => {
    let data = {
        busid:business.id,
        consignee:values.name,
        address:values.addressDetail,
        mobile:values.tel,
        code:values.areaCode,
        status:values.isDefault === true ? 1 : 0
    }

    let result = await proxy.$Api.addressAdd(data);

    if(result && result.code === 1)
    {
        proxy.$showNotify({
            type:'success',
            message:result.msg,
            duration:2000,
            onClose:() => {
                proxy.$router.back();
            }
        });

        return;
    }else{
        proxy.$showNotify({
            type:'warning',
            message:result.msg,
            duration:2000
        });

        return;
    }
}

const onBack = () => {
    proxy.$router.back();
}
</script>